﻿using System;
using System.Net.Mail;
using System.Text;
using System.Web;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using System.Web.UI;

namespace ASPNETVN.PORTAL.Controls
{
    public partial class Register : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Random rnd = new Random();
                int digit1 = rnd.Next(0, 9);
                imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                int digit2 = rnd.Next(0, 9);
                imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                int digit3 = rnd.Next(0, 9);
                imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                int digit4 = rnd.Next(0, 9);
                imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
            }
        }

        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if (chkAgree.Checked)
            {
                try
                {
                    if (ViewState["Digit"].ToString() == txtSecurityCode.Text)
                    {
                        User user = new User();
                        user.Name = txtName.Text;
                        user.Email = txtEmail.Text;
                        user.Password = txtPassword.Text;
                        int returnID = user.Register();
                        if (returnID > 0)
                        {
                            Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)Context.Items["Portal"];
                            string portalTitle = portal.Title;
                            string portalUrl = Global.GetApplicationPath();


                            string code = EncryptHelper.Encrypt(txtEmail.Text);
                            StringBuilder body = new StringBuilder();
                            body.AppendFormat("Xin chao <strong>{0}</strong>!<br><br>", txtName.Text);
                            body.AppendFormat("Ban da dang ky lam thanh vien cua <a href='{0}'><strong>{1}</strong></a> voi dia chi Email la {2}.<br><br>", portalTitle, portalUrl, txtEmail.Text);
                            body.AppendFormat("Thong tin dang nhap Web Site {0} cua ban nhu sau: <br>- Email dang nhap: {1}<br>- Mat khau dang nhap: {2}<br><br>", portal.Title, txtEmail.Text, txtPassword.Text);
                            //body.Append("De kich hoat tai khoan, ban lam theo 1 trong 2 cach sau:<br>");
                            //body.AppendFormat("Cach 1: Kich chuot vao lien ket: <a href='{0}/ActiveUser.aspx?ActiveCode={1}' target='_blank'>Kich hoat tai khoan</a><br>", portalUrl, code);
                            //body.AppendFormat("Cach 2: Copy ma kich hoat: <strong>{0}</strong> roi dan vao o ma kich hoat o dia chi <a href='{1}/ActiveUser.aspx' target='_blank'>Dia chi kich hoat tai khoan</a><br>", code, portalUrl);
                            body.Append("<br>Tran trong!");
                            body.Append("<br><br><hr>");
                            body.AppendFormat("<br><strong><a href='{0}'>{1}</a></strong>", portalUrl, portalTitle);
                            body.AppendFormat("<br>Web Master: {0}", portal.WebMasterName);
                            body.AppendFormat("<br>Mobile: {0}", portal.WebMasterPhone);
                            
                            MailHelper.SendMail(portal.WebMasterEmail, txtEmail.Text, "", "", portalTitle + " - Thong tin dang ky thanh vien", body.ToString(), MailPriority.High);
                            Response.Redirect("~/RegisterCompleted.aspx");
                        }
                        else if (returnID == -1)
                        {
                            txtMessage.Text = "Email này đã đăng ký rồi. Vui lòng kiểm tra lại!";
                        }
                    }
                    else
                    {
                        txtSecurityCode.Text = string.Empty;
                        HttpContext.Current.Response.Write("<script>alert('Mã số kiểm tra không chính xác');</script>");
                        Random rnd = new Random();
                        int digit1 = rnd.Next(0, 9);
                        imgNumber1.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit1);

                        int digit2 = rnd.Next(0, 9);
                        imgNumber2.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit2);

                        int digit3 = rnd.Next(0, 9);
                        imgNumber3.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit3);

                        int digit4 = rnd.Next(0, 9);
                        imgNumber4.ImageUrl = string.Format("~/Images/Icons/Numbers/{0}.jpg", digit4);

                        ViewState["Digit"] = digit1 + "" + digit2 + "" + digit3 + "" + digit4;
                    }
                }
                catch (Exception ex)
                {
                    txtMessage.Text = "Có lỗi do hệ thống: " + ex.Message;
                }
            }
            else
            {
                txtMessage.Text = "Bạn chưa chọn đồng ý thỏa thuận làm thành viên của WEB SITE.";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}